using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Pampa.CRUD.Exceptions;

using System.Web.UI;
using System.Web.UI.WebControls;

using Pampa.CRUD.Core;
using Pampa.CRUD.UI.CustomControls;

namespace Pampa.CRUD.Configuration
{
    /// <summary>
    /// Esta clase representa la definicion de un item a visualizar en el CRUD de una entidad
    /// </summary>
    public class ViewItem : VisibleItem
    {
        Label label = new Label();
        string format;

        public ViewItem() : base()
        {
        }

        public ViewItem(ViewItem sourceItem) : base(sourceItem)
        {
        }

        #region Properties

        public string Format
        {
            get { return format; }
            set { format = value; }
        }

        public override ItemType ItemType
        {
            get { return ItemType.Label; }
        }

        public string StringValue
        {
            get 
            {
                if (this.Value == null)
                    return " - ";
                else if (this.Value is IIdDescription)
                    return ((IIdDescription)this.Value).Description;
                else if (String.IsNullOrEmpty(this.Format))
                    return this.Value.ToString();
                else
                {
                    if (this.Value is Boolean)
                        return (((bool)this.Value) ? Resources.EntityEdit.True : Resources.EntityEdit.False);
                    else if (this.Value is DateTime)
                        return ((DateTime)this.Value).ToString(this.Format);
                    else if (this.Value is float)
                        return ((float)this.Value).ToString(this.Format);
                    else if (this.Value is double)
                        return ((double)this.Value).ToString(this.Format);
                    else 
                        return this.Value.ToString();
                }
            }
        }
        
        public override Control Control
        {
            get
            {
                label.Width = this.ValueWidth;
                label.CssClass = "label";
                label.Text = this.StringValue;

                return label;
            }
        }

        #endregion

        #region Public Functions

        public override VisibleItem Clone()
        {
            return new ViewItem(this);
        }

        public override void LoadXml(XmlNode node)
        {            
            base.LoadXml(node);

            if (node.Attributes["Format"] != null)
                this.Format = node.Attributes["Format"].Value;

            // Los view items no pueden incluir validaciones.
            if (node.ChildNodes.Count > 0)
            {
                throw new ConfigurationException("Los items del tipo 'ViewItem' no pueden incluir validaciones.");
            }
        }

        #endregion
    }
}
